package com.vvlive.caibao.smartcity.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.vvlive.caibao.smartcity.bean.EventCard;

import java.util.List;

import smartcity.vvlive.caibao.com.smartc.R;

/**
 * Created by zhj on 2015/12/17.
 * 日历日视图的时间卡片
 */
public class EventCardView extends ViewGroup implements View.OnClickListener, View.OnLongClickListener {

    private List<EventCard> mEventCards;
    private OnEventClickListener onEventClickListener;
    private OnEventLongClickListener onEventLongClickListener;

    public EventCardView(Context context) {
        this(context, null);
    }

    public EventCardView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

        int width = this.getMeasuredWidth();

        int count = this.getChildCount();
        if (mEventCards != null && !mEventCards.isEmpty()) {
            for (int i = 0; i < count; i++) {
                EventCard eventCard = mEventCards.get(i);
                View view = this.getChildAt(i);
                TextView title = (TextView) view.findViewById(R.id.event_card_title);
                if (title != null) {
                    title.setText(eventCard.getTitle());
                }
                TextView time = (TextView) view.findViewById(R.id.event_card_time);
                if (time != null) {
                    time.setText(eventCard.getSecondTitle());
                }
                TextView room = (TextView) view.findViewById(R.id.event_card_room);
                if (room != null) {
                    room.setText(eventCard.getDescription());
                }

                int offsetTop = eventCard.getOffsetTop();
                view.layout(0, offsetTop, width, offsetTop + eventCard.getHeight());
                view.setTag(R.id.event_card_title, i);
                view.setOnClickListener(this);
                view.setOnLongClickListener(this);
            }
            // make it "wrap_content", no matter how high it is.
            this.setMinimumHeight(this.getChildAt(count - 1).getBottom());
        }

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        final int count = getChildCount();
        for (int i = 0; i < count; i++) {
            getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    public void setEvents(List<EventCard> eventCards) {
        mEventCards = eventCards;
        this.removeAllViews();
        for (EventCard eventCard : mEventCards) {
            View event = inflate(getContext(), R.layout.view_event_card, null);
            this.addView(event);
        }
    }

    public void addEvent(EventCard eventCard) {
        mEventCards.add(eventCard);
        this.addView(inflate(getContext(), R.layout.view_event_card, null));
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag(R.id.event_card_title);
        if (this.onEventClickListener != null) {
            this.onEventClickListener.onEventClick(v, mEventCards.get(position), position);
        }
    }

    @Override
    public boolean onLongClick(View v) {
        int position = (int) v.getTag(R.id.event_card_title);
        if (this.onEventLongClickListener != null) {
            return this.onEventLongClickListener.onEventLongClick(v, mEventCards.get(position), position);
        }
        return false;
    }

    public interface OnEventClickListener {
        void onEventClick(View view, EventCard card, int position);
    }

    public interface OnEventLongClickListener {
        boolean onEventLongClick(View view, EventCard card, int position);
    }

    public void setOnEventClickListener(OnEventClickListener onEventClickListener) {
        this.onEventClickListener = onEventClickListener;
    }

    public void setOnEventLongClickListener(OnEventLongClickListener onEventLongClickListener) {
        this.onEventLongClickListener = onEventLongClickListener;
    }
}

